package com.example.demo;

import org.springframework.boot.system.ApplicationHome;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Copyfright(C),2022-2022,复兴元宇科技有限公司
 * FileName:BackFIrTestCtl
 * Author:yz
 * Date:2022/3/14 11:23 上午
 * Description:
 * History:
 * <author>  <time> <version> <desc>
 * 作者       时间    版本      描述
 */
@RestController
public class BackFIrTestCtl {
    //对象接收测试
    @RequestMapping(value = "/addobject", method = RequestMethod.POST)
    public YZResult<Product> reciveObj(@RequestBody Product product) throws Exception {
        System.out.println(product.describe);
        return YZResponse.makeOKRsp(product);
    }

    @RequestMapping("/addnew")
    public YZResult<String> addmany(HttpServletRequest request) throws Exception {
        System.out.println("name:" + request.getParameter("name"));
        System.out.println("info:" + request.getParameter("info"));
        return YZResponse.makeOKRsp("you are grate");
    }

    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    public YZResult<String> uploadInfo(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) {
        if (file.isEmpty()) {
            return YZResponse.makeErrRsp("请选择文件");
        }
        try {
            System.out.println("-----------------------");
            System.out.println("test file =  ");
            System.out.println(file);
            byte[] bytes = file.getBytes();
            String rootDir = getSavePath() + "\\";
            Path path = Paths.get("/Users/macbookpro/Desktop/demo/src/main/resources/static/upload/fineagent.jar");
//            File file1 = new File(String.valueOf(path));
//            if (!file1.getParentFile().exists()) {
//                System.out.println("i crate the file");
//                file1.getParentFile().mkdirs();
//            } else {
//                System.out.println("file exists !");
//            }
//            System.out.println(path);
            Files.write(path, bytes);
            return YZResponse.makeOKRsp("you are successful upload file");
        } catch (IOException e) {
            System.out.println("-----------------------");
            e.printStackTrace();
            System.out.println("-----------------------");
        }
        return YZResponse.makeErrRsp("upload info fail");
    }

    public String getSavePath() {
        // 这里需要注意的是ApplicationHome是属于SpringBoot的类
        ApplicationHome applicationHome = new ApplicationHome(this.getClass());
        // 保存目录位置根据项目需求可随意更改
        return applicationHome.getDir().getParentFile()
                .getParentFile().getAbsolutePath() + "/src/main/resources/static/upload";
    }

    long l = System.currentTimeMillis();
    Date date = new Date(l);
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
    String nyr = dateFormat.format(date);

    @RequestMapping("/upimage")
    public YZResult<String> upimage(@RequestParam("upimage") MultipartFile file, RedirectAttributes redirectAttributes) {
        if (file.getOriginalFilename().endsWith(".jpg") || file.getOriginalFilename().endsWith(".png") || file.getOriginalFilename().endsWith(".gif")) {
            try {
                byte[] bytes = file.getBytes();
                String basic = getSavePath() + "/";
                String S = nyr + "Math.random() % 100" + "/" + file.getOriginalFilename();
                String path = basic + S;
                File file1 = new File(String.valueOf(path));
                if (!file1.getParentFile().exists()) {
                    System.out.println("i crate the file");
                    file1.getParentFile().mkdirs();
                } else {
                    System.out.println("file exists !");
                }
                Files.write(Paths.get(path), bytes);
                return YZResponse.makeOKRsp("successful");
            } catch (Exception e) {
                e.printStackTrace();
            }
            return YZResponse.makeErrRsp("error");
        } else {
            return YZResponse.makeErrRsp("image type can only support png or jpg or gif,please check it in detail");
        }
    }


}
